package com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.beerandjesus.sportofkings.handicapper.model.Medication;
import static com.beerandjesus.sportofkings.handicapper.model.Medication.*;

import com.beerandjesus.sportofkings.handicapper.model.EquipmentChange;
import static com.beerandjesus.sportofkings.handicapper.model.EquipmentChange.*;
import com.beerandjesus.sportofkings.handicapper.model.RaceType;
import com.beerandjesus.sportofkings.handicapper.model.TrackSurface;
import static com.beerandjesus.sportofkings.handicapper.model.TrackSurface.*;


/**
 * Utility for converting codes form Brisnet data files (single file format)
 * into enumeration types.
 * 
 * More complex objects should be handled by their own factories where
 * necessary.
 * @author mstevens
 */
public class ConversionUtility {

	public RaceType getRaceType(String string) {
		if (StringUtils.isEmpty(string)) {
			return RaceType.EMPTY;
		}
		try {
			return RaceType.valueOf(string.toUpperCase());
		} catch (NullPointerException e) {
			return RaceType.EMPTY;
		} catch (IllegalArgumentException e) {
			return RaceType.UNDEFINED;
		}
	}
	
	private static final Map<String, TrackSurface> SURFACES = new HashMap<String, TrackSurface>();
	static {
		SURFACES.put("D", DIRT);
		SURFACES.put("T", TURF);
		SURFACES.put("d", INNER_DIRT);
		SURFACES.put("t", INNER_TURF);
		SURFACES.put("s", STEEPLE);
		SURFACES.put("h", HUNT);
	}

	public TrackSurface getTrackSurface(String string) {
		if (StringUtils.isEmpty(string)) {
			return TrackSurface.EMPTY;
		}
		TrackSurface s = SURFACES.get(string);
		if (s != null) {
			return s;
		} else {
			return TrackSurface.UNDEFINED;
		}
	}

	public Medication getMedication(String string) {
		if (StringUtils.isEmpty(string)) {
			return Medication.EMPTY;
		}
		int i;
		try {
			i = Integer.valueOf(string);
		} catch (Exception e) {
			return Medication.UNDEFINED;
		}
		switch (i) {
			case 0:
				return NONE;
			case 1:
				return LASIX;
			case 2:
				return BUTE;
			case 3:
				return BUTE_LAS;
			case 4:
				return LAS_FIRST;
			case 5:
				return BUTE_LAS_FIRST;
			case 9:
				return Medication.UNAVAILABLE;
			default:
				return Medication.UNDEFINED;
		}
	}

	public EquipmentChange getEquipmentChange(String string) {
		if (StringUtils.isEmpty(string)) {
			return EquipmentChange.EMPTY;
		}
		int i;
		try {
			i = Integer.valueOf(string);
		} catch (Exception e) {
			return EquipmentChange.UNDEFINED;
		}
		switch (i) {
			case 0:
				return NO_CHANGE;
			case 1:
				return BLINKERS_ON;
			case 2:
				return BLINKERS_OFF;
			case 9:
				return EquipmentChange.UNAVAILABLE;
			default:
				return EquipmentChange.UNDEFINED;
		}
	}

}
